let express=require(`express`)
let db=require(`./4.封装函数db`)
let router=express.Router()
router.get(`/a/b/lis`,(req,res)=>{
    db(`select *from categories order by id`,(err,result)=>{
        if(err) return res.send({code:1000,msg:`错误`})
        res.send({code:200,msg:`ok`,data:result})
    })
})

router.post(`/a/b/add`,(req,res)=>{
    let {name,slug}=req.body
    db(`insert into categories set name='${name}',slug='${slug}';`,(err,result)=>{
        if(err||res.affectedRows===0)return res.send({code:2000,msg:'错误'})
        res.send({code:200,msg:`创建成功`})
    })
})
router.get(`/a/b/search`,(req,res)=>{
    let {id}=req.query
    db(`select * from categories where id=${id}`,(err,result)=>{
        if(err||result.length===0) return res.send({code:2000,msg:`错误`})
        res.send({code:200,msg:"ok",data:result})
    })
}
)
router.post(`/a/b/edit`,(req,res)=>{
    let {name,id,slug}=req.body
    db(`update categories set name='${name}',slug='${slug}' where id=${id};`,(err,result)=>{
        if(err||result.affectedRows===0) return res.send({code:22000,msg:`错误`})
        res.send({code:200,msg:`编辑成功`})
    })
})
router.post(`/a/b/delete`,(req,res)=>{
    let {id}=req.body
    db(`delete from categories where id=${id};`,(err,result)=>{
        if(err||result.affectedRows===0) return res.send({code:2000,msg:`错误`})
        res.send({code:200,msg:'删除成功'})
    })
})



module.exports=router